﻿using Microsoft.EntityFrameworkCore;
using ProductManagement.Domain.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProductManagement.Infrastructure.Data
{
    public class ProductDbContext: DbContext
    {
        public ProductDbContext(DbContextOptions<ProductDbContext> options) : base(options)
        {
        }

        public DbSet<Product> Products { get; set; }

        public DbSet<User> Users { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            //配置用户表映射
            modelBuilder.Entity<User>(entity =>
            {
                entity.ToTable("users");

                entity.HasKey(e => e.Id);

                entity.Property(e => e.Id)
                    .HasColumnName("id")
                    .ValueGeneratedOnAdd();

                entity.Property(e => e.Name)
                    .HasColumnName("name")
                    .IsRequired()
                    .HasMaxLength(50);
                entity.Property(e => e.Sex)
                   .HasColumnName("sex")
                   .IsRequired();
                entity.Property(e => e.Birthday)
                   .HasColumnName("birthday")
                   .IsRequired();
                entity.Property(e => e.Phone)
                   .HasColumnName("phone")
                   .IsRequired()
                   .HasMaxLength(11);
                entity.Property(e => e.CreatedDate)
                   .HasColumnName("created_date");
                entity.Property(e => e.UpdatedDate)
                   .HasColumnName("updated_date");
            });

            // 配置实体映射
            modelBuilder.Entity<Product>(entity =>
            {
                entity.ToTable("products");

                entity.HasKey(e => e.Id);

                entity.Property(e => e.Id)
                    .HasColumnName("id")
                    .ValueGeneratedOnAdd();

                entity.Property(e => e.Name)
                    .HasColumnName("name")
                    .IsRequired()
                    .HasMaxLength(100);

                entity.Property(e => e.Description)
                    .HasColumnName("description")
                    .HasMaxLength(500);

                entity.Property(e => e.Price)
                    .HasColumnName("price")
                    .IsRequired()
                    .HasColumnType("decimal(10, 2)");

                entity.Property(e => e.Stock)
                    .HasColumnName("stock")
                    .IsRequired();

                entity.Property(e => e.CreatedAt)
                    .HasColumnName("created_at");

                entity.Property(e => e.UpdatedAt)
                    .HasColumnName("updated_at");
            });
        }
    }
}
